package ControlLayer;
import ModelLayer.*;

import DBLayer.*;

public class ProductCtr {
	
	private static ProductCtr instance;
	
	private ProductCtr()
	{
		
	}

	public static ProductCtr getInstance()
	{
		
		if(instance==null)
			instance=new ProductCtr();
		
		return instance;
	}
	
	public void insertNew(String pName,double pPrice, double sPrice, double rPrice, String origin, int minStock, int currentStock, String supCvr, int id)
	{DBSupplier dbSupplier=new DBSupplier();
		Supplier supplier=dbSupplier.findSupplier(supCvr,false);
		//Supplier supplier= new Supplier(supCvr); 
		Product prodObj= new Product(pName,pPrice,sPrice,rPrice,origin,minStock,currentStock,supplier,id);

		DbConnection1.startTransaction();
		DBProduct dbProduct= new DBProduct();
		try {
			dbProduct.insertProduct(prodObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!DbConnection1.commitTransaction())
			DbConnection1.rollbackTransaction();
		
	}
	
	
	public static void main(String args[])
	{
		ProductCtr ctr= new ProductCtr();
		ctr.insertNew("coke&fanta",33,44,55,"romania",10,50,"ion",1232332312);
	}
	
	
	
		
	public Product findProduct(int id)
	{
		DBProduct dbProduct= new DBProduct();
		return dbProduct.findProduct(id, true);
	}
	
	
	
	public void updateProduct(String pName, double pPrice, double sPrice, double rPrice, String origin, int minStock, int currentStock, String supCVR, int prodId)
	{
		 Supplier supplier= new Supplier(supCVR);
		Product product =new Product(pName, pPrice, sPrice, rPrice, origin, minStock, currentStock, supplier,prodId );
		DBProduct dbProduct= new DBProduct();
		dbProduct.updateProduct(product);
		
	}
	
	
	
	
	
}
